package com.xinghen.security.core.validate.code.email;

import com.xinghen.security.core.properties.SecurityProperties;
import com.xinghen.security.core.validate.code.ValidateCode;
import com.xinghen.security.core.validate.code.ValidateCodeGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;

import java.util.Arrays;

/**
 * @author xinghen
 * @ClassName: EmailValidateCodeGenerator
 * @Package: com.xinghen.security.core.validate.code.email
 * @Description 邮箱验证码生成器
 * @create 2018-04-18 15:44
 */
@Slf4j
@Component
public class EmailValidateCodeGenerator implements ValidateCodeGenerator {

    @Autowired
    private SecurityProperties securityProperties;

    @Override
    public ValidateCode generate(ServletWebRequest servletWebRequest) {
        String[] emails = ServletRequestUtils.getStringParameters(servletWebRequest.getRequest(), "email");
        String code = Arrays.toString(emails);
        return new ValidateCode(code, securityProperties.getCode().getEmail().getExpireIn());
    }
}
